package com.fancy.projecty.A_流程相关;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * L_回退都某个流程
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class G_回退到某个流程 {


	/**
	 * 回退到某个流程 测试有效果
	 * https://blog.csdn.net/qq_27467601/article/details/54694773
	 */
	@Test
	public void c_回退到某个环节() {
		String processInstanceId = "130001";  // 流程实例id
		// 取得当前任务.当前任务节点
		String destTaskkey = "_3";  // bpmn流程图的  userTask 的id

		// HistoricTaskInstance currTask = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
		Map<String, Object> variables;
		ExecutionEntity entity = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(processInstanceId).singleResult();
		ProcessDefinitionEntity definition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService).getDeployedProcessDefinition(entity.getProcessDefinitionId());
		variables = entity.getProcessVariables();
		//当前活动环节
		ActivityImpl currActivityImpl = definition.findActivity(entity.getActivityId());
		//目标活动节点
		ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition).findActivity(destTaskkey);
		if (currActivityImpl != null) {
			//所有的出口集合
			List<PvmTransition> pvmTransitions = currActivityImpl.getOutgoingTransitions();
			List<PvmTransition> oriPvmTransitions = new ArrayList<PvmTransition>();
			for (PvmTransition transition : pvmTransitions) {
				oriPvmTransitions.add(transition);
			}
			//清除所有出口
			pvmTransitions.clear();
			//建立新的出口
			List<TransitionImpl> transitionImpls = new ArrayList<TransitionImpl>();
			TransitionImpl tImpl = currActivityImpl.createOutgoingTransition();
			tImpl.setDestination(nextActivityImpl);
			transitionImpls.add(tImpl);

			List<Task> list = taskService.createTaskQuery().processInstanceId(entity.getProcessInstanceId())
					.taskDefinitionKey(entity.getActivityId()).list();
			for (Task task : list) {
				taskService.complete(task.getId(), variables);
				historyService.deleteHistoricTaskInstance(task.getId());
			}

			for (TransitionImpl transitionImpl : transitionImpls) {
				currActivityImpl.getOutgoingTransitions().remove(transitionImpl);
			}

			for (PvmTransition pvmTransition : oriPvmTransitions) {
				pvmTransitions.add(pvmTransition);
			}
		}
	}


	/*历史记录的查询*/
	@Autowired
	private HistoryService historyService;


	@Autowired
	/*管理流程定义,部署相关的Service*/
	private RepositoryService repositoryService;


	@Autowired
	/*执行管理、包括启动、推进、删除流程实例等操作*/
	private RuntimeService runtimeService;


	@Autowired
	/*任务管理,可以做任务的查询的*/
	private TaskService taskService;


}
